package com.lekang.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lekang.common.annotation.Log;
import com.lekang.common.core.controller.BaseController;
import com.lekang.common.core.domain.AjaxResult;
import com.lekang.common.enums.BusinessType;
import com.lekang.system.domain.AppointmentRegistration;
import com.lekang.system.service.IAppointmentRegistrationService;
import com.lekang.common.utils.poi.ExcelUtil;
import com.lekang.common.core.page.TableDataInfo;

/**
 * 预约登记Controller
 * 
 * @author 刘卓
 * @date 2025-10-13
 */
@RestController
@RequestMapping("/system/registration")
public class AppointmentRegistrationController extends BaseController
{
    @Autowired
    private IAppointmentRegistrationService appointmentRegistrationService;

    /**
     * 查询预约登记列表
     */
//    @PreAuthorize("@ss.hasPermi('system:registration:list')")
    @GetMapping("/list")
    public TableDataInfo list(AppointmentRegistration appointmentRegistration)
    {
        startPage();
        List<AppointmentRegistration> list = appointmentRegistrationService.selectAppointmentRegistrationList(appointmentRegistration);
        return getDataTable(list);
    }

    /**
     * 导出预约登记列表
     */
//    @PreAuthorize("@ss.hasPermi('system:registration:export')")
//    @Log(title = "预约登记", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AppointmentRegistration appointmentRegistration)
    {
        List<AppointmentRegistration> list = appointmentRegistrationService.selectAppointmentRegistrationList(appointmentRegistration);
        ExcelUtil<AppointmentRegistration> util = new ExcelUtil<AppointmentRegistration>(AppointmentRegistration.class);
        util.exportExcel(response, list, "预约登记数据");
    }

    /**
     * 获取预约登记详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:registration:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(appointmentRegistrationService.selectAppointmentRegistrationById(id));
    }

    /**
     * 新增预约登记
     */
//    @PreAuthorize("@ss.hasPermi('system:registration:add')")
//    @Log(title = "预约登记", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AppointmentRegistration appointmentRegistration)
    {
        return toAjax(appointmentRegistrationService.insertAppointmentRegistration(appointmentRegistration));
    }

    /**
     * 修改预约登记
     */
//    @PreAuthorize("@ss.hasPermi('system:registration:edit')")
//    @Log(title = "预约登记", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AppointmentRegistration appointmentRegistration)
    {
        return toAjax(appointmentRegistrationService.updateAppointmentRegistration(appointmentRegistration));
    }

    /**
     * 删除预约登记
     */
//    @PreAuthorize("@ss.hasPermi('system:registration:remove')")
//    @Log(title = "预约登记", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(appointmentRegistrationService.deleteAppointmentRegistrationByIds(ids));
    }

    /**
     * 到院核销 - 将待上门状态改为已完成
     */
//    @PreAuthorize("@ss.hasPermi('system:registration:complete')")
//    @Log(title = "预约登记", businessType = BusinessType.UPDATE)
    @PutMapping("/complete/{id}")
    public AjaxResult complete(@PathVariable Long id)
    {
        int result = appointmentRegistrationService.markCompleted(id, new java.util.Date());
        if (result > 0) {
            return success("到院核销成功");
        } else {
            return error("到院核销失败，请确认预约状态为待上门");
        }
    }

    /**
     * 取消预约 - 将待上门状态改为已取消
     */
//    @PreAuthorize("@ss.hasPermi('system:registration:cancel')")
//    @Log(title = "预约登记", businessType = BusinessType.UPDATE)
    @PutMapping("/cancel/{id}")
    public AjaxResult cancel(@PathVariable Long id)
    {
        int result = appointmentRegistrationService.cancelById(id);
        if (result > 0) {
            return success("取消预约成功");
        } else {
            return error("取消预约失败，请确认预约状态为待上门");
        }
    }

    /**
     * 获取预约统计信息
     */
    @GetMapping("/statistics")
    public AjaxResult getStatistics()
    {
        return success(appointmentRegistrationService.getAppointmentStatistics());
    }

    /**
     * 手动执行过期处理
     */
//    @PreAuthorize("@ss.hasPermi('system:registration:expire')")
//    @Log(title = "预约登记", businessType = BusinessType.UPDATE)
    @PostMapping("/expire")
    public AjaxResult expireOvertime()
    {
        int count = appointmentRegistrationService.expireOvertime();
        return success("处理过期预约成功，共处理 " + count + " 条记录");
    }
}
